/**
 *
 */
package com.javademos.train.dynamicdatasource.view.controller;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.javademos.train.dynamicdatasource.entity.DemoEntity;
import com.javademos.train.dynamicdatasource.service.DemoService;

/**
 * @author 踩坑填坑
 * @Description: [ 演示控制器 ]
 * @Email: 120987555@qq.com
 * @Date: 2017年9月1日 下午2:59:05
 * @Version: V1.0
 */
@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    /**
     * @描述: 测试ID不存在的查询单个数据
     * @date: 2017年9月17日 下午11:23:00
     */
    @RequestMapping(value = "/demo/findOne")
    public void testFindOne() {
        DemoEntity exist = null;

        exist = demoService.findOneByJpa(1000000L);
        if (exist != null) {
            System.out.println("1000000查询到数据: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("1000000没有数据!");
        }

        exist = demoService.findOneByJpa(-1L);
        if (exist != null) {
            System.out.println("-1查询到数据: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("-1没有数据!");
        }

        exist = demoService.findOneByJpa(null);
        if (exist != null) {
            System.out.println("查询到数据: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("没有数据!");
        }
    }

    @RequestMapping(value = "/demo/jpa")
    public ModelAndView jpa() {
        List<DemoEntity> list = demoService.findAllByJpa();
        if (list != null) {
            System.out.println("jpa()结果集个数: " + list.size());
        } else {
            System.out.println("jpa()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJpa(1L);
        if (exist != null) {
            System.out.println("jpa()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jpa()单个数查询无结果! ");
        }
        return null;
    }

    @RequestMapping(value = "/demo/jpa2")
    public ModelAndView jpa2() {
        List<DemoEntity> list = demoService.findAllByJpaByDs2();
        if (list != null) {
            System.out.println("jpa2()结果集个数: " + list.size());
        } else {
            System.out.println("jpa2()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJpa2(1L);
        if (exist != null) {
            System.out.println("jpa2()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jpa2()单个数查询无结果! ");
        }
        return null;
    }

    @RequestMapping(value = "/demo/jpa3")
    public ModelAndView jpa3() {
        List<DemoEntity> list = demoService.findAllByJpaByDs3();
        if (list != null) {
            System.out.println("jpa3()结果集个数: " + list.size());
        } else {
            System.out.println("jpa3()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJpa3(1L);
        if (exist != null) {
            System.out.println("jpa3()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jpa3()单个数查询无结果! ");
        }
        return null;
    }

    @RequestMapping(value = "/demo/jdbc")
    public ModelAndView jdbc() {
        List<DemoEntity> list = demoService.findAllJdbc();
        if (list != null) {
            System.out.println("jdbc()结果集个数: " + list.size());
        } else {
            System.out.println("jdbc()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJdbc(1L);
        if (exist != null) {
            System.out.println("jdbc()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jdbc()单个数查询无结果! ");
        }
        return null;
    }

    @RequestMapping(value = "/demo/jdbc2")
    public ModelAndView jdbc2() {
        List<DemoEntity> list = demoService.findAllJdbcByDs2();
        if (list != null) {
            System.out.println("jdbc2()结果集个数: " + list.size());
        } else {
            System.out.println("jdbc2()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJdbc2(1L);
        if (exist != null) {
            System.out.println("jdbc2()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jdbc2()单个数查询无结果! ");
        }
        return null;
    }

    @RequestMapping(value = "/demo/jdbc3")
    public ModelAndView jdbc3() {
        List<DemoEntity> list = demoService.findAllJdbcByDs3();
        if (list != null) {
            System.out.println("jdbc3()结果集个数: " + list.size());
        } else {
            System.out.println("jdbc3()结果集个数为空! ");
        }

        DemoEntity exist = demoService.findOneByJdbc3(1L);
        if (exist != null) {
            System.out.println("jdbc3()单个数: " + ToStringBuilder.reflectionToString(exist));
        } else {
            System.out.println("jdbc3()单个数查询无结果! ");
        }
        return null;
    }

}
